package yscmd

import (
	"context"
)

type ICommandBuilder interface {
	// Init 初始化命令
	Init(ctx context.Context)

	// Command 获取此命令
	Command() *Command

	// SetChildCommand 利用 builder 设置子命令
	SetChildCommand(builder ICommandBuilder) bool

	// SetFlag 设置 Flag 标签
	SetFlag(IFlagBuilder, ...string) bool
}

func SetChildCommand(parent ICommandBuilder, child ICommandBuilder) bool {
	child.Init(parent.Command().Context())
	return parent.Command().AddCommand(child.Command())
}

type CommandBuilderCore struct {
	C *Command
}

func (b *CommandBuilderCore) Init(ctx context.Context) {
	b.C = NewCommandWithDefaultFunc()
	b.C.SetContext(ctx)
}

func (b *CommandBuilderCore) SetChildCommand(builder ICommandBuilder) bool {
	return SetChildCommand(b, builder)
}

func (b *CommandBuilderCore) Command() *Command {
	return b.C
}

func (b *CommandBuilderCore) SetFlag(builder IFlagBuilder, group ...string) bool {
	return SetFlag(b, builder, group...)
}

func (b *CommandBuilderCore) SetChildCommands(builders ...ICommandBuilder) {
	for _, builder := range builders {
		b.SetChildCommand(builder)
	}
}

func (b *CommandBuilderCore) SetFlags(builders ...IFlagBuilder) {
	for _, builder := range builders {
		b.SetFlag(builder)
	}
}

func (b *CommandBuilderCore) SetFlagsWithGroup(group string, builders ...IFlagBuilder) {
	for _, builder := range builders {
		b.SetFlag(builder, group)
	}
}
